//
//  MessageViewController.m
//  test
//
//  Created by kepuG on 17/1/20.
//  Copyright © 2017年 text. All rights reserved.
//

#import "MessageViewController.h"
#import "Person.h"
#import "Car.h"
#import "DataBase.h"

@interface MessageViewController ()


@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    btn.frame = CGRectMake(10, 100, 70, 30);
    [btn addTarget:self action:@selector(deleteAtion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    UIButton *updabtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [updabtn setTitle:@"修改" forState:UIControlStateNormal];
    updabtn.frame = CGRectMake(100, 100, 70, 30);
    [updabtn addTarget:self action:@selector(updabtnAtion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:updabtn];

    
    UIButton *carbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [carbtn setTitle:@"添加车" forState:UIControlStateNormal];
    carbtn.frame = CGRectMake(10, 160, 70, 30);
    [carbtn addTarget:self action:@selector(addCarAtion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:carbtn];

    
    UIButton *printbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [printbtn setTitle:@"打印" forState:UIControlStateNormal];
    printbtn.frame = CGRectMake(100, 160, 70, 30);
    [printbtn addTarget:self action:@selector(printbtnAtion) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:printbtn];

    
    // Do any additional setup after loading the view.
}

-(void)printbtnAtion{
    
    DataBase *base = [DataBase shareDataBase];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[base getAllData]];
    Person *deleP = [arr lastObject];
    [base getAllCarsFromPerson:deleP];
    DebugLog(@"======car==%@",[base getAllCarsFromPerson:deleP]);
}

-(void)addCarAtion{

    DataBase *base = [DataBase shareDataBase];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[base getAllData]];
    Person *deleP = [arr lastObject];

    Car *car = [[Car alloc] init];
    car.own_id = deleP.ID;
    
    NSArray *brandArray = [NSArray arrayWithObjects:@"大众",@"宝马",@"奔驰",@"奥迪",@"保时捷",@"兰博基尼", nil];
    NSInteger index = arc4random_uniform((int)brandArray.count);
    car.brand = [brandArray objectAtIndex:index];
    
    car.price = arc4random_uniform(1000000);
    [[DataBase shareDataBase] addCar:car toPerson:deleP];
    
//    self.carArray = [[DataBase sharedDataBase] getAllCarsFromPerson:self.person];



}

-(void)updabtnAtion{

    DataBase *base = [DataBase shareDataBase];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[base getAllData]];
    Person *updateP = [arr lastObject];
    updateP.name = [NSString stringWithFormat:@"%@_%@",updateP.name,@"修改"];
    
    [base updatePerson:updateP];
}

-(void)deleteAtion{
    DataBase *base = [DataBase shareDataBase];
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[base getAllData]];
    Person *deleP = [arr lastObject];
    
    DebugLog(@"%@",deleP.ID);
    
    [base deletePerson:deleP];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) addData{

    int nameRandom = arc4random_uniform(1000);
    NSInteger ageRandom  = arc4random_uniform(100) + 1;
    
    NSString *name = [NSString stringWithFormat:@"person_%d号",nameRandom];
    NSInteger age = ageRandom;

    Person *p1 = [[Person alloc] init];
    p1.name = name;
    p1.age = age;
    
    DataBase *base = [DataBase shareDataBase];
    [base addPerson:p1];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self addData];

    DataBase *base = [DataBase shareDataBase];
    NSLog(@"=====%@",[base getAllData]);
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
